package com.example.choupaiapp.main.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.BlackListBean;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.main.constant.Url;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2019/11/19.
 */

public class NoWatchOtherActivity extends CommonActivity {

    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.gridview)
    GridView gridview;
    private QuickAdapter<BlackListBean.PageBean.ListBean> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_watch_other);
    }

    @Override
    protected void initView() {
        setTitle("不看TA的动态");
        rightTv.setText("编辑");

        quickAdapter = new QuickAdapter<BlackListBean.PageBean.ListBean>(context, R.layout.item_no_watch_other) {
            @Override
            protected void convert(BaseAdapterHelper helper, BlackListBean.PageBean.ListBean item) {
                ImageView delete = helper.getView(R.id.delete);
                if (item.getIsDelete() == 0) {
                    delete.setVisibility(View.INVISIBLE);
                } else {
                    delete.setVisibility(View.VISIBLE);
                }

                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        showDialog(context, "删除中...");
                        finalOkGo.requestHead(RequestType.POSTHEAD, Url.CANCEL_BLACK_LIST + item.getByUserId(), null, Url.CANCEL_BLACK_LIST, new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                                dismissDialog();
                                if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                    quickAdapter.remove(item);
                                    notifyDataSetChanged();
                                } else {
                                    if(sendCodeBean.getCode() == Contacts.TOKEN_EMPTY){
                                    }else{
                                        showImageToas(sendCodeBean.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                    }
                });

                helper.setText(R.id.name, item.getNickname());

                CircleImageView head_photo = helper.getView(R.id.head_photo);
                Glide.with(context)
                        .load(item.getAvatar()+ Config.IMAGE_LIM)
                        .dontAnimate()
                        .placeholder(R.mipmap.icon_no_head)
                        .into(head_photo);
            }
        };

        gridview.setAdapter(quickAdapter);

        gridview.setOverScrollMode(View.OVER_SCROLL_NEVER);

    }

    @Override
    protected void initData() {
        showDialog(context, null);
        HttpParams params = new HttpParams();
        params.put("limit", 10000);
        params.put("page", 1);
        finalOkGo.requestHead(RequestType.POSTHEAD, Url.BLACK_LIST, params, Url.BLACK_LIST, new Callback<BlackListBean>() {
            @Override
            public void onSuccess(BlackListBean blackListBean) {
                dismissDialog();
                if (blackListBean.getCode() == Contacts.SUCCESS) {
                    quickAdapter.clear();
                    quickAdapter.addAll(blackListBean.getPage().getList());
                    quickAdapter.notifyDataSetChanged();
                } else {
                    if(blackListBean.getCode() == Contacts.TOKEN_EMPTY){
                    }else{
                        showImageToas(blackListBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @OnClick(R.id.right_tv)
    public void onViewClicked() {
        if (Utils.isDoubleClick()) {
            return;
        }
        if (rightTv.getText().equals("编辑")) {
            rightTv.setText("完成");
            for (int i = 0; i < quickAdapter.getCount(); i++) {
                quickAdapter.getItem(i).setIsDelete(1);
            }
            quickAdapter.notifyDataSetChanged();
        } else {
            rightTv.setText("编辑");
            for (int i = 0; i < quickAdapter.getCount(); i++) {
                quickAdapter.getItem(i).setIsDelete(0);
            }
            quickAdapter.notifyDataSetChanged();
        }


    }
}
